package com.educate.ucenter.rpc.provide.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.educate.common.utils.converter.ListUtils;
import com.educate.common.utils.exception.AppException;
import com.educate.ucenter.dao.FamilyMemberDao;
import com.educate.ucenter.model.bo.FamilyMemberBO;
import com.educate.ucenter.model.po.FamilyMember;
import com.educate.ucenter.rpc.api.FamilyMemberService;
import lombok.extern.log4j.Log4j2;
import org.springframework.data.domain.Page;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

@Log4j2
@Service(
        version = "${api.service.version}",
        application = "${dubbo.application.id}",
        protocol = "${dubbo.protocol.id}",
        registry = "${dubbo.registry.id}"
)
public class FamilyMemberServiceImpl implements FamilyMemberService {

    @Resource FamilyMemberDao familyMemberDao;

    @Override
    public void create(FamilyMemberBO bo) {
        familyMemberDao.insert(bo);
    }

    @Override
    public void delete(FamilyMemberBO bo) {
        familyMemberDao.delete(bo);
    }

    @Override
    public void delete(String familyMemberId) {
        familyMemberDao.deleteById(familyMemberId);
    }

    @Override
    public FamilyMemberBO detail(String familyMemberId) {
        Optional<FamilyMember> optional = familyMemberDao.findById(familyMemberId);
        if (!optional.isPresent()) {
            throw new AppException("不存在的实例");
        }
        return new FamilyMemberBO(optional.get());
    }

    @Override
    public FamilyMemberBO detail(FamilyMemberBO bo) {
//        return detail(bo.getOrganizationId());
        return null;
    }

    @Override
    public void update(FamilyMemberBO bo) {
//        Optional<FamilyMember> optional = familyMemberDao.findById(bo.getFamilyMemberId());
//        if (!optional.isPresent()) {
//            throw new AppException("不存在的实例");
//        }
//        BeanUtils.copyPropertiesNonEmpty(bo, optional.get());
//        familyMemberDao.save(optional.get());
    }

    @Override
    public Page<FamilyMemberBO> query(FamilyMemberBO bo) {
        return familyMemberDao.query(bo, bo.pageable()).map(FamilyMemberBO::new);
    }

    @Override
    public List<FamilyMemberBO> list(FamilyMemberBO bo) {
        List<FamilyMember> result = familyMemberDao.findAll();
        return ListUtils.map(result, FamilyMemberBO::new);
    }
}
